<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class WechatOauth
{


    protected $auth;

    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->isWechatBrowser()) {
            if (!$this->auth->check()) {
                if (!session('socialite')) {
                    session(['redirect_url' => $request->fullUrl()]);
                    return redirect(route('oauth.login', ['driver' => 'wechat']));
                }
            }
        }
        return $next($request);
    }
    protected function isWechatBrowser()
    {
        if (strpos(request()->userAgent(), 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }
}
